export default {
        unparam: function (str, params) {
            if (!str) return;
            params = params || {};
            str.replace(/\/$/, '').replace(/([^?=&]+)=([^&#]*)/g, function (m, key, value) {
                params[key] = decodeURIComponent(value);
            });
            return params;
        },
        /* eg.
         util.formatDate('2014-03-24', '-1m')    => 2014/02/24
         util.formatDate('2014-03-24', '-2d')    => 2014/03/22
         util.formatDate('2014-03-24', '/')      => 2014/03/22
         util.formatDate(null, '/')              => 2014/10/15
         util.formatDate(null, '')               => 20141015
         util.formatDate(null, 'datetime')       => 2015/08/04 17:41:48
         */
        formatDate: function (date, str, div) {
            if (!date) {
                date = new Date();
            }
            else if (typeof date === 'number') {
                date = new Date(date);
            }
            else if (typeof date === 'string') {
                date = new Date(/(\d{4})\D?(\d{2})\D?(\d{2})/.exec(date).splice(1).join('/'));
            }
            var y, m, d, H, M, S, time = '';

            if (typeof str === 'string') {
                str = str.toLowerCase();
                if (str === 'datetime') {
                    H = date.getHours();
                    M = date.getMinutes();
                    S = date.getSeconds();
                    time = ' ' + (H > 9 ? H : '0' + H) + ':' + (M > 9 ? M : '0' + M) + ':' + (S > 9 ? S : '0' + S);
                }
                else if (/\d(y|m|d)$/i.test(str)) {
                    var n = str.substr(0, str.length - 1);
                    switch (str.substr(-1)) {
                        case 'y':
                            date.setFullYear(date.getFullYear() + (+n));
                            break;
                        case 'm':
                            date.setMonth(date.getMonth() + (+n));
                            break;
                        case 'd':
                            date.setDate(date.getDate() + (+n));
                            break;
                    }
                } else {
                    div = div || str;
                }
            }
            if (div === undefined) div = '/';
            y = date.getFullYear();
            m = date.getMonth() + 1;
            d = date.getDate();

            return y + div + (m > 9 ? m : '0' + m) + div + (d > 9 ? d : '0' + d) + time;
        },
        // 格式化金额
        formatMoney: function (value, unit) {
            if (value === undefined) value = 0;
            var parts = Number(value).toFixed(2).toString().split('.');
            return parts[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,') + '.' + parts[1] + (unit ? ' ' + unit : '');
        },
        formatNumber:function (value) {
            if (value === undefined) value = 0;
            var parts = Number(value).toString().split('.');
            return parts[0].replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1');
        },
        // 精确浮点数
        formatFloat: function (num, length) {
            if (typeof length !== 'number') length = 2;
            var n = Math.pow(10, length);
            return (num * n / n).toFixed(length);
        },
        // 手机号、邮箱地址打码
        formatName: function (str) {
            str = str ? String(str) : "";
            return str.replace(/^(\d{3})\d{4}(\d{4})$/, '$1****$2').replace(/^(.{3})[^@]*(@.+)$/, '$1****$2');
        },

        storage: {
            set: function (key, value) {
                key = key.replace(/\//g, '_');
                if (value === undefined) return;
                localStorage.setItem(key, JSON.stringify(value));
            },
            get: function (key) {
                key = key.replace(/\//g, '_');
                return JSON.parse(localStorage.getItem(key));
            },
            remove: function (key) {
                key = key.replace(/\//g, '_');
                localStorage.removeItem(key);
            },
            clear: function () {
                localStorage.clear();
            }
        },

        session: {
            set: function (key, value) {
                key = key.replace(/\//g, '_');
                if (value === undefined) return;
                sessionStorage.setItem(key, JSON.stringify(value));
            },
            get: function (key) {
                key = key.replace(/\//g, '_');
                return JSON.parse(sessionStorage.getItem(key));
            },
            remove: function (key) {
                key = key.replace(/\//g, '_');
                sessionStorage.removeItem(key);
            },
            clear: function () {
                sessionStorage.clear();
            }
        },

        cookie: {
            set: function (key, value, expires, path, domain, secure) {
                if (value === null) value = "";
                if (typeof expires === "number") {
                    expires = new Date(+new Date() + 86400000 * expires);
                }
                document.cookie = key + "=" + escape(value)
                    + ((expires) ? "; expires=" + expires.toUTCString() : "")
                    + ((path) ? "; path=" + path : "; path=/")
                    + ((domain) ? "; domain=" + domain : "")
                    + ((secure) ? "; secure" : "");
            },
            get: function (key) {
                var arr = document.cookie.match(new RegExp("(^| )" + key + "=([^;]*)(;|$)"));
                return arr != null ? unescape(arr[2]) : null;
            },
            remove: function (key, path, domain) {
                if (this.get(key)) {
                    this.set(key, "", -365);
                }
            }
        },

        base64: {
            encode: function (str) {
                return window.btoa(encodeURIComponent(escape(typeof str !== 'string' ? '' : str)));
            },
            decode: function (str) {
                return unescape(decodeURIComponent(window.atob(typeof str !== 'string' ? '' : str)));
            }
        }
    };
